package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsStatService;
import com.qf.vo.DayStatis;
import com.qf.vo.StatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author YDW
 * projectName:shop-manage
 * time:2022/5/23 20:44
 * description:
 */
@Service
public class DtsStatServiceImpl implements DtsStatService {

    @Autowired
    StatMapper statMapper;

    @Autowired
    DtsOrderMapper orderMapper;

    @Autowired
    DtsUserMapper userMapper;

    @Override
    public StatVo user() {
        StatVo statVo=new StatVo();
        String[] columns=new String[]{"day","users"};
        statVo.setColumns(columns);

        List<DayStatis> dayStatis = statMapper.getOrderAmts();
        for (DayStatis dayStati : dayStatis) {
            Map<String,Object> map=new HashMap<>();
            map.put("day",dayStati.getDayStr());
            QueryWrapper qw=new QueryWrapper<>();
            qw.eq("DATE_FORMAT(add_time,\"%Y-%m-%d\")",dayStati.getDayStr());
            map.put("users",userMapper.selectCount(qw));
            statVo.add(map);
        }
        return statVo;
    }

    @Override
    public StatVo order() {
        StatVo statVo=new StatVo();
        String[] columns=new String[]{"day","orders","customers","amount","pcr"};
        statVo.setColumns(columns);

        List<DayStatis> dayStatis = statMapper.getOrderAmts();
        BigDecimal avg=null;
        for (DayStatis dayStati : dayStatis) {
            Map<String,Object> map=new HashMap<>();
            map.put("day",dayStati.getDayStr());
            QueryWrapper qw=new QueryWrapper<>();
            qw.eq("DATE_FORMAT(add_time,\"%Y-%m-%d\")",dayStati.getDayStr());
            map.put("orders",orderMapper.selectCount(qw));
            map.put("customers ",dayStati.getCnts());
            map.put("amount ",dayStati.getAmts());
            avg=dayStati.getAmts().divide(BigDecimal.valueOf(dayStati.getCnts()),2,BigDecimal.ROUND_HALF_UP);
            map.put("pcr",avg);


            statVo.add(map);
        }
        return statVo;
    }
}
